package com.example.myprojects;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager.widget.ViewPager;

import com.example.myprojects.fragment.HomeFragment;
import com.example.myprojects.fragment.VideoFragment;
import com.example.myprojects.fragment.WoDeFragment;
import com.example.myprojects.service.MyService;
import com.example.myprojects.uitl.HttpUtil;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener,ViewPager.OnPageChangeListener {

    private ViewPager pager;
    private RadioButton rb1;
    private RadioButton rb2;
    private RadioButton rb3;
    private RadioGroup rg;
    HomeFragment homeFragment;
    VideoFragment videoFragment;
    WoDeFragment woDeFragment;
    MyFragAdpater adpater;
    FragmentManager fm;
    FragmentTransaction ft;
    List<Fragment> fragmentList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initFrag();
    }
    void getData(){
        final HttpUtil httpUtil=new HttpUtil();


    }

    private void initView() {
        pager = (ViewPager) findViewById(R.id.pager);
        rb1 = (RadioButton) findViewById(R.id.rb1);
        rb2 = (RadioButton) findViewById(R.id.rb2);
        rb3 = (RadioButton) findViewById(R.id.rb3);
        rg = (RadioGroup) findViewById(R.id.rg);
        rg.setOnCheckedChangeListener(this);
        pager.addOnPageChangeListener(this);
    }
    void initFrag(){
        fragmentList=new ArrayList<>();
        fm=getSupportFragmentManager();
        ft=fm.beginTransaction();
        homeFragment=new HomeFragment();
        videoFragment=new VideoFragment();
        woDeFragment=new WoDeFragment();
        fragmentList.add(homeFragment);
        fragmentList.add(videoFragment);
        fragmentList.add(woDeFragment);
        adpater=new MyFragAdpater(fm,0,fragmentList);
        pager.setCurrentItem(0);
        pager.setAdapter(adpater);

    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId){
            case R.id.rb1:
                pager.setCurrentItem(0);
                break;
            case  R.id.rb2:
                pager.setCurrentItem(1);
                break;
            case R.id.rb3:
                pager.setCurrentItem(2);
                break;
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        switch (position){
            case 0:
                rb1.setChecked(true);
                break;
            case 1:
                rb2.setChecked(true);
                break;
            case 2:
                rb3.setChecked(true);
                break;
        }

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

}
